package com.moons.uvcdemo.util;

import android.util.Log;

import com.moons.uvcdemo.config.DemoConfig;
import com.moons.uvcdemo.model.UserFace;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class FileUtil {
    private static final String TAG = "FileUtil";

    private FileUtil() {
    }

    public static FileUtil getInstance() {
        return FileUtilHolder.INSTANCE;
    }

    private static class FileUtilHolder {
        private static final FileUtil INSTANCE = new FileUtil();
    }

    public byte[] loadPictureFile(File file) {
        if (!file.exists()) {
            Log.e(TAG, "readUserFaceFromSDCard: target file don't exist");
            return null;
        }
        FileInputStream fis;

        try {
            fis = new FileInputStream(file);
            int lenght = fis.available();
            byte[] buffer = new byte[lenght];
            fis.read(buffer);

            return buffer;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    private void saveFeatureInStringToSDCard(UserFace userFace) {
        File dir = new File(DemoConfig.FeaturesPath);
        File file = new File(dir.getPath(), userFace.getName() + "InString.txt");
        if (!file.exists()) {
        } else {
            file.delete();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < userFace.getFeature().length; i++) {
            stringBuilder.append(userFace.getFeature()[i] + ",");
        }
        String content = stringBuilder.toString();

        FileOutputStream fos = null;
        BufferedOutputStream bufferedOutputStream = null;


        try {
            //获得FileOutputStream
            fos = new FileOutputStream(file);
            bufferedOutputStream = new BufferedOutputStream(fos);
            bufferedOutputStream.write(content.getBytes());
            bufferedOutputStream.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭创建的流对象
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }


        }

    }

    public void saveUserFaceToSDCard(UserFace userFace) {
        File dir = new File(DemoConfig.FeaturesPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        saveFeatureInStringToSDCard(userFace);

        File file = new File(dir.getPath(), userFace.getName() + ".txt");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        BufferedOutputStream bufferedOutputStream = null;

        FileOutputStream fos = null;
        try {
            //获得FileOutputStream
            fos = new FileOutputStream(file);


            byte[] bytes = Arrays.copyOf(userFace.getFeature(), userFace.getFeature().length);
            bufferedOutputStream = new BufferedOutputStream(fos);
            // 往文件所在的缓冲输出流中写byte数据
            bufferedOutputStream.write(bytes);
            // 刷出缓冲输出流，该步很关键，要是不执行flush()方法，那么文件的内容是空的。
            bufferedOutputStream.flush();


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭创建的流对象
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
    }

    public byte[] readUserFaceFromFile(File file) {
        if (!file.exists()) {
            Log.e(TAG, "readUserFaceFromSDCard: target file don't exist");
            return null;
        }
        FileInputStream fis;

        try {
            fis = new FileInputStream(file);
            int lenght = fis.available();
            byte[] buffer = new byte[lenght];
            fis.read(buffer);

            return buffer;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static float[] byteArr2floatArr(byte[] bArr) {
        ByteArrayInputStream bas = new ByteArrayInputStream(bArr);
        DataInputStream ds = new DataInputStream(bas);
        float[] fArr = new float[bArr.length / 4];  // 4 bytes per float
        for (int i = 0; i < fArr.length; i++) {
            try {
                fArr[i] = ds.readFloat();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return fArr;
    }
}
